package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.net.PostHistory;
import longcaisuyun.longcai.com.suyunbean.HistoryBean;

//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//        pulllist.onPullDownRefreshComplete();

//        pulllist.onPullUpRefreshComplete();

/************************************************************/

public class HistoryActivity extends Activity implements View.OnClickListener {

    PullToRefreshListView pulllist;
    LinearLayout fanhui_lin;
    String nextpage = "1";
    String startpage = "1";
    List<HistoryBean> list = new ArrayList<HistoryBean>();
    HistoryAdapter h = new HistoryAdapter(HistoryActivity.this, list);
    String ispage = "1";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();

        HttpPost(startpage);

    }

    private void ViewInit() {
        LinearLayout   fanhui_lin= (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(this);
        pulllist = (PullToRefreshListView) findViewById(R.id.pulllist);
        pulllist.setScrollLoadEnabled(false);
        pulllist.setPullLoadEnabled(true);
        pulllist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                HttpPost(startpage);

            }

            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
        ListView listView = pulllist.getRefreshableView();
        listView.setDivider(null);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(HistoryActivity.this, MonthActivity.class);
                intent.putExtra("code", list.get(position).getCode());
                startActivity(intent);
            }
        });
        View view = View.inflate(HistoryActivity.this, R.layout.list_footer, null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        view.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (!ispage.equals(nextpage)) {
                    HttpPost(nextpage);
                } else {
                    Toast.makeText(HistoryActivity.this, "已经加载完毕", Toast.LENGTH_SHORT).show();
                }
            }
        });
        listView.addFooterView(view);
        listView.setAdapter(h);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_lin:
                HistoryActivity.this.finish();
                break;
        }
    }

    public void HttpPost(String page) {
        MyApplication.myviewutil.startProgressDialog(HistoryActivity.this);
        new PostHistory(MyApplication.myPreferences.readUid(), page,"", new AsyCallBack<PostHistory.Info>() {
            @Override
            public void onSuccess(String toast, int type, PostHistory.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("1")) {
                    JSONArray jsonArray = info.jsonArray;
                    addlist(jsonArray);
                    nextpage = info.nextpage;
                    ispage = info.ispage;
                    h.notifyDataSetChanged();
                } else {
                    Toast.makeText(HistoryActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();

                }
                pulllist.onPullDownRefreshComplete();
                MyApplication.myviewutil.stopProgressDialog();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                MyApplication.myviewutil.stopProgressDialog();
                pulllist.onPullDownRefreshComplete();
                Toast.makeText(HistoryActivity.this, "获取数据失败,请检查您的网络设置", Toast.LENGTH_SHORT).show();
            }

        }).execute(HistoryActivity.this);
    }

    public void addlist(JSONArray jsonArray) {
        list.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            HistoryBean historyBean = new HistoryBean();
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(j);
                historyBean.setTitle(jsonObject.getString("title"));
                historyBean.setCode(jsonObject.getString("code"));
                list.add(historyBean);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
